
package controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JSlider;
import javax.swing.JTextField;

import gui.workspace.Workspace;
import model.AbstractPen;
import model.Bearing;
import model.Model;
import model.Turtle;

/**
 * Controller class to handle actions and inputs dealing with properties component of system
 * 
 * @author Troy Ferrell
 * @author Shun Fan
 */
public class PropertiesController extends AbstractController
{
	Map<Integer, Turtle> turtleMap = new HashMap<Integer, Turtle>();
	private Turtle selectedTurtle;
	private int selectedTurtleId;
	
	public PropertiesController(Model model) 
	{
		super(model);		
	}
	
	public void setSelectedTurtle(int turtleId) {
		selectedTurtleId = turtleId;
		selectedTurtle = turtleMap.get(Integer.valueOf(turtleId));
	}
	
	public void setStroke(Model model, JComboBox box) {
		System.out.println("stroke is set to " + (String) box.getSelectedItem());
	}
	
	public void getStroke(JComboBox box) {
		System.out.println("stroke is updated to " + (String) box.getSelectedItem());
	}
	
	public void setTrailWidth(Model model, JSlider slider) {
		if( !slider.getValueIsAdjusting() ) {
			System.out.println(slider.getValue());
		}
		//System.out.println("this is working ");
		selectedTurtle.myPen.setThickness(slider.getValue());
		
		model.notifyObservers(Model.TURTLE_OBSERVER);
	}
	
	public void getTrailWidth(JSlider slider) {
		System.out.println("Slider is updating");
		
		//slider.setValue(selectedTurtle.myPen.getThickness());
	}

	public void setBackgroundImage(Model model, JButton button) {
		JFileChooser chooser = new JFileChooser();
		String path;
		int returnVal = chooser.showOpenDialog(button);
		if (returnVal == JFileChooser.APPROVE_OPTION) {
			File file = chooser.getSelectedFile();
			path = file.getAbsolutePath();
			System.out.println(path);
			myModel.setBackground(path);
		}
		//model.getBackground();
		
		
	}

	public void setVisibility(Model model, JComboBox box) {
		System.out.println("visibilty is set to " + (String) box.getSelectedItem());
		int i = box.getSelectedIndex();
		switch(i) {
			case 0: selectedTurtle.showTurtle(); //System.out.println("show");
			case 1: selectedTurtle.hideTurtle();
		}
	}	
		
	public void getVisibility(JComboBox box) {
		
	}

	public void setShape(Model model, JComboBox box) {
		System.out.println("shape is set to " + (String) box.getSelectedItem());
		String shape = (String) box.getSelectedItem();
		String path = "src/resources/images/" + shape + ".png";
		myModel.getTurtle(selectedTurtleId).setShape(path);
	}
	
	public void getShape(JComboBox box) {
		System.out.println("shape is updated to " + (String) box.getSelectedItem());
	}
	
	public String[] getTurtles(Model model) {
		turtleMap.clear();
		ArrayList<Turtle> turtles = (ArrayList<Turtle>) model.getAllTurtles();
		String[] turtleIds = new String[turtles.size()];
		for(int i = 0; i < turtles.size(); i++) {
			turtleMap.put(Integer.valueOf(i), turtles.get(i));
			turtleIds[i] = Integer.valueOf(i).toString();
		}
		selectedTurtle = turtleMap.get(Integer.valueOf(0)); 		
		return turtleIds;
	}
	// TODO add methods & implementations here
	
	public void setWorldHeight(Model model, JTextField field) {
		System.out.println(field.getText());
		int worldHeight = Integer.parseInt(field.getText());
	}
	
	public void getWorldHeight(JTextField field) {
		System.out.println(field.getText());
		int worldHeight = Integer.parseInt(field.getText());
	}
	
	public void setWorldWidth(Model model, JTextField field) {
		System.out.println(field.getText());
		int worldHeight = Integer.parseInt(field.getText());
	}
	
	public void getWorldWidth(JTextField field) {
		System.out.println(field.getText());
		int worldHeight = Integer.parseInt(field.getText());
	}
	
	public void setX(Model model, JTextField field) {
		System.out.println("x set to: " + field.getText());
		Bearing originalBearing = selectedTurtle.getBearing();
		int originalY = originalBearing.getY();
		Double originalDirection = originalBearing.getDirection();
		selectedTurtle.setBearing(Integer.parseInt(field.getText()), originalY, originalDirection);
	}
	
	public void getX(JTextField field) {
		int x = selectedTurtle.getBearing().getX();
		System.out.println("x: " + x);
		field.setText(String.valueOf(x));
	}
	
	public void setY(Model model, JTextField field) {
		System.out.println("y set to: " + field.getText());
		Bearing originalBearing = selectedTurtle.getBearing();
		int originalX = originalBearing.getX();
		Double originalDirection = originalBearing.getDirection();
		selectedTurtle.setBearing(originalX, Integer.parseInt(field.getText()), originalDirection);
	}
	
	public void getY(JTextField field) {
		int y = selectedTurtle.getBearing().getY();
		System.out.println("y: " + y);
		field.setText(String.valueOf(y));
	}
}
